package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.common.ReggieConstants;
import com.itheima.dto.SetmealDishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    // 新增套餐
    @PostMapping
    @CacheEvict(value = ReggieConstants.SETMEAL_LIST_REDIS_KEY,allEntries = true)
    public R<String> saveSetmeal(@RequestBody  SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return  R.success("新增成功");
    }

    @GetMapping("/page")
     public R<Page<SetmealDto>> pageSetmeal(Integer page , Integer pageSize,String name){
        Page<SetmealDto> pageInfo = setmealService.pageSetmealWithCate(page,pageSize,name);
        return  R.success(pageInfo);
    }

    // 删除套餐
    @DeleteMapping
    @CacheEvict(value = ReggieConstants.SETMEAL_LIST_REDIS_KEY,allEntries = true)
    public R<String> removeDishList(@RequestParam List<Long> ids){
        log.info("ids: {}",ids);
        setmealService.removeSetmealList(ids);
        return  R.success("删除成功");
    }

    @GetMapping("/list")
    @Cacheable(value = ReggieConstants.SETMEAL_LIST_REDIS_KEY,key = "#categoryId+':'+#status")
    public  R<List<Setmeal>> geiSetmealList( Long categoryId , Integer status){
        List<Setmeal> setmealList =  setmealService.getgeiSetmealList(categoryId,status);
        return  R.success(setmealList);
    }

    // 批量起售
    @PostMapping("/status/1")
    public R<String> statusSetmeal1(@RequestParam List<Long> ids){
        // update setmeal set status = 1  where id in (?,?,?)
        log.info("ids: {}",ids);
        setmealService.updateSetmeal1(ids);
        return  R.success("起售成功");
    }

    // 批量停售
    @PostMapping("/status/0")
    public R<String> statusSetmeal0(@RequestParam List<Long> ids){
        // update setmeal set status = 0  where id in (?,?,?)
        log.info("ids: {}",ids);
        setmealService.updateSetmeal0(ids);
        return  R.success("停售成功");
    }

    // 修改套餐
    @PutMapping
    @CacheEvict(value = ReggieConstants.SETMEAL_LIST_REDIS_KEY,allEntries = true)
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto ){
        setmealService.updateSetmeal(setmealDto);
        return  R.success("修改成功");
    }

    // 修改时候显示数据
    @GetMapping("/{id}")
    public R<SetmealDto> getDish(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getWithFlavor(id);
        return R.success(setmealDto);
    }

     // 点击套餐显示菜品
    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> getDishId(@PathVariable Long id){

        List<SetmealDish> semealDishDto = setmealService.getDish(id);
        return R.success(semealDishDto);

    }



}
